import {
  ADD_COUNTER,
  ADD_TO_CART,
  CHECK_OUT,
  SAVA_USER_INFO
} from './mutation-types'

export default {
  addCart (context, payload) {
    return new Promise((resolve, reject) => {
      //payload是新增的商品
      let oldProduct = context.state.cartList.find(
        item => item.iid === payload.iid
      )
      //判断oldProduct
      if (oldProduct) {
        // oldProduct.count++;
        context.commit(ADD_COUNTER, oldProduct)
        resolve('继续添加成功')
      } else {
        payload.count = 1
        // state.cartList.push(payload)
        context.commit(ADD_TO_CART, payload)
        resolve('添加成功')
      }
    })
  },
  //下单
  order (context, payload) {
    return new Promise((resolve, reject) => {
      context.commit(CHECK_OUT, payload)
      resolve('结算成功')
    })
  },
  //保存登录信息
  saveUserInfo (context, payload) {
    return new Promise(resolve => {
      context.commit(SAVA_USER_INFO, payload)
      resolve('登录成功')
    })
  }
}
